import { Component, ChangeDetectorRef } from '@angular/core';
import { NavController, ActionSheetController } from '@ionic/angular';
import { Storage } from '../providers/Storage';

import { ShopService } from '../services/shop.service';
import { Helper } from '../providers/Helper';
import { ToastService } from '../providers/toast/toast.service';
import * as constants from '../providers/Constants'
import { AuthService } from 'src/app/services/auth.service';
import { Utils } from '../providers/Utils';
import { OrderService } from '../services/order.service';

@Component({
  selector: 'app-tab4',
  templateUrl: 'tab4.page.html',
  styleUrls: ['tab4.page.scss']
})
export class Tab4Page {
  userPhone:string='Login'
  avatarUrl:string='';
  isShow:boolean=false;
  constructor(private nav:NavController,private ref:ChangeDetectorRef,private order:OrderService,private actionSheetController:ActionSheetController,private mShopService: ShopService,
    private mHelper: Helper,
    public service: AuthService,
    private toast: ToastService,) {
    }
  login(){
    this.nav.navigateForward('login');
  }
  isShowMsg(){
    if(Storage.sessionStorage.get('isNewMsg')){
      this.isShow=true;
    }else{
      this.isShow=false;
    }
    //console.log(' this.isShow', this.isShow);
    return this.isShow;
  }
  ionViewDidEnter() {
    this.ref.detectChanges()
    this.ref.markForCheck()
    this.getUserInfo();

  }
  exit(){
    this.presentActionSheet();
  }
  onPay(){
    this.nav.navigateForward('system-message',{
      queryParams:{
        type:'1',
      }
    });
  }
  queryOrder(type){
    this.nav.navigateForward('order-list',{
      queryParams:{
        orderType:type
      },
      animated:true
    })
  }
  toUrl(){
    if (this.userPhone=='Login'){
      this.nav.navigateForward('login');
    }else{
      this.nav.navigateForward('info',{animated:true});
    }
  }

  async presentActionSheet() {
    const actionSheet = await this.actionSheetController.create({
      header: 'Log out?',
      mode:'ios',
      cssClass:'exit-action-sheet',
      buttons: [{
        text: 'YES',
        handler: () => {
          this.nav.navigateForward('login');
          Storage.localStorage.clear();
          console.log('Delete clicked');
        }
      }, {
        text: 'NO',
        role: 'cancel',
        handler: () => {
          console.log('Cancel clicked');
        }
      }]
    });
    await actionSheet.present();
  }



  getUserInfo() {
    this.service.getUserInfo().subscribe(res => {
      // 保存成功返沪上一页，否则弹出提示框
      if (res.retCode == '000000') {
        if (res.info) {
         let thirdLogin= Storage.localStorage.get('thirdLogin');
          if (res.info.avatarUrl) {
            // if (thirdLogin) {
            //   this.avatarUrl=Utils.formatUrl(res.info.avatarUrl);
            // }else{
              this.avatarUrl=this.order.getImageUrl()+res.info.avatarUrl;
            // }
          }
          // this.userPhone=res.info.userPhone
          // if (thirdLogin) {
          //   this.userPhone=res.info.nickName;   
          // }else{
            this.userPhone=res.info.userNickName;
         // }
       
          // Storage.localStorage.set('userInfo', res.info);
          // Storage.localStorage.set('userPhone', res.info.userPhone);
        } 
      } else if (res.retMsg) {
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    });
  }



}
